import logging

import pytest

from resource.pom.settings import Settings


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击设置应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.进入设置界面')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击隐私')
        device.click(*self.app.privacy_item)
        logging.info('预期结果2.进入隐私')
        device.refresh_layout()
        device.assert_text_exist('证书与凭据')

        logging.info('步骤3.点击证书与凭证')
        device.click_element(device.get_element_by_text('证书与凭据'))
        logging.info('预期结果3.进入证书与凭证')
        device.refresh_layout()
        device.assert_text_exist('CA证书')
        device.assert_text_exist('凭据列表')
        device.assert_text_exist('从存储设备安装')
        device.assert_text_exist('删除所有证书与凭据')

        logging.info('步骤4.点击从存储设备安装')
        device.click_element(device.get_element_by_text('从存储设备安装'))
        logging.info('预期结果4.进入从存储设备安装界面，存在“CA证书”和“用户证书凭据”选项')
        device.refresh_layout()
        device.assert_text_exist('CA证书')
        device.assert_text_exist('系统凭据')
        device.assert_text_exist('用户凭据')
        ca_certificate = device.get_element_by_text('CA证书')
        user_credential = device.get_element_by_text('用户凭据')

        logging.info('步骤5.点击CA证书')
        device.click_element(ca_certificate)
        logging.info('预期结果5.页面权限弹框提示，点击允许可进入文件类型页面，页面显示无异常')
        device.refresh_layout()
        device.assert_type_exist('Dialog')
        device.assert_text_exist('根证书')
        device.assert_text_exist('取消')
        device.assert_text_exist('继续')
        device.click_element(device.get_element_by_text('继续'))
        device.refresh_layout()
        device.assert_type_exist('ModalPage')
        device.assert_type_exist('SheetWrapper')
        device.assert_type_exist('SheetPage')
        device.assert_text_exist('我的手机')
        # device.assert_text_exist('Download')
        # device.assert_text_exist('Documents')

        logging.info('步骤6.返回存储设备安装界面后，点击用户证书凭据')
        device.go_back()
        device.wait(0.5)
        device.click_element(user_credential)
        logging.info('预期结果6.进入文件类型页面，页面显示无异常')
        device.refresh_layout()
        device.assert_type_exist('ModalPage')
        device.assert_type_exist('SheetWrapper')
        device.assert_type_exist('SheetPage')
        device.assert_text_exist('我的手机')
        # device.assert_text_exist('Download')
        # device.assert_text_exist('Documents')
